<%@ page contentType="application/json; charset=UTF-8" pageEncoding="UTF-8" errorPage="/error.jsp"%>
<%@page import="dbexp.model.Trigger"%>
<%@page import="dbexp.model.db.DbUser"%>
<%@page import="dbexp.connection.DbMetaData"%>
<%@page import="dbexp.connection.DbConnection"%>
<%@page import="org.json.JSONObject"%>
<%
DbUser u = (DbUser)session.getAttribute("USER");
String name = request.getParameter("name");
String schema = request.getParameter("schema");
String source = request.getParameter("source");
String action = request.getParameter("action");
boolean brak = (name==null || name.trim().equals("null")) && (schema==null || schema.trim().equals("null"));
Trigger ob =  new Trigger(name,schema);
if(!brak && action.equals("create")||action.equals("update")){
	String[] linie = source.split("\n");
	for(int i =0;i<linie.length;i++){
		ob.addLine(linie[i]+"\n");
	}
	int status = DbMetaData.operateObject(u,ob,DbConnection.CREATE);
}
	ob = DbMetaData.getTrigger(u, name, schema);
	JSONObject my_out = new JSONObject();
	my_out.put("success", true).
	put("data",new JSONObject().
			put("name", ob.getName()).
			put("schema",ob.getSchema()).
			put("when",ob.getWhen()).
			put("table",ob.getString("table_name")).
			put("fer",ob.isForEachRow()).
			put("insert",ob.isInsert()).
			put("delete",ob.isDelete()).
			put("update",ob.isUpdate()).
			put("tableOwner",ob.getString("table_owner")).
			put("source",ob.getContent()).
			put("error",ob.getErrorString()));
	
out.clearBuffer();%><%=my_out%>